import { _decorator, Component, Node, UITransform, Vec3 } from 'cc';
const {ccclass, property} = _decorator;

@ccclass('Joystick')
export default class Joystick extends Component {

    @property(UITransform)
    Trans: UITransform = null;
    @property
    Radius: number = 100;
    @property(Node)
    Center: Node = null;
    public _direction:Vec3 = new Vec3();
    public get Direction() : Vec3 {
        return this._direction;
    }
    
    public SetCenter(worldPos:Vec3)
    {
        this.Trans.convertToNodeSpaceAR(worldPos, this._direction);
        if(Vec3.len(this._direction) > this.Radius)
        {
            this._direction.normalize();
            this.Center.setPosition(this._direction.normalize().multiplyScalar(this.Radius));
        }
        else
        {
            this.Center.setPosition(this._direction);
            this._direction.normalize();
        }
    }
}

